const express = require('express')
const morgan = require('morgan')
const cros = require('cors')
const app = new express()
require('./models/')
const logger = require('./logger')
const productRoutes = require('./routes/productRoutes')
const orderRoutes = require('./routes/orderRoutes')
const paymentRoutes = require('./routes/paymentRoutes')
app.use(morgan('dev'))
app.use(cros())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.get('/', (req, res) => {
  logger.info('wechatpay')
  res.send('wechatpay')
})
require('./swagger')(app)
app.use('/api/products', productRoutes)
app.use('/api/orders', orderRoutes)
app.use('/api/payment', paymentRoutes)
// 如果有四个参数，第一个是错误
app.use((error, req, res, next) => {
  logger.error('Error', error)
  res.status(500).send(`Internal Server Error`)
})
app.use((error, req, res, next) => {
  res.status(400).send(`404: Page Not Found`)
})
module.exports = app
